package com.goodwill.androidtest.genericity;

import android.os.Build;

import androidx.annotation.RequiresApi;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.nio.file.Paths.get;

public class SwapTest {
    public static void main(String[] args) {
//        swapTest();
//        hashMapTest();
        java8Test();
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private static void java8Test() {
        String path = "D:\\workspace\\codes\\jiahemeikang\\AndroidTest\\app\\new_file.txt";
//        File file = new File("./test.txt");
//        if (file.exists()) {
//            try {
//                file.createNewFile();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        try(OutputStream stream = new FileOutputStream(new File(path))) {
//            stream.write("123456".getBytes());
//            stream.flush();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


//        try {
//            byte[] bytes = Files.readAllBytes(get(path));
//            String string = new String(bytes);
//            System.out.println(string);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(12);
        arrayList.add(88);
        arrayList.add(45);
        arrayList.add(99);
        arrayList.add(100);
        arrayList.add(440);

        testMap(arrayList);
//        testFilter(arrayList);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private static void testMap(List<Integer> arrayList) {
        Stream<Integer> stream = arrayList.stream();
        List<String> collect = arrayList.stream().map(String::valueOf).filter(str -> str.startsWith("8")).collect(Collectors.toList());
        System.out.println(collect.toString());
//        stream.limit(100);
//        stream.skip(20);
        /**
         * {@link Stream#concat(Stream<? extends T> a, Stream<? extends T> b)}
         */
        Stream<Integer> first = Stream.of(12, 34, 45);
        Stream<String> second = Stream.of("23", "56", "78");
        /*List<String> concatResult = (List<String>) */
        Stream.concat(first, second).collect(Collectors.toList());
//        System.out.println("concatResult:" + concatResult.toString());

        method1();
    }

    private static synchronized void method1() {

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private static void testFilter(List<Integer> arrayList) {
        Predicate<Integer> filter1 = integer -> integer < 90;
        Predicate<Integer> filter2 = integer -> integer > 20;
        long count = arrayList.stream().filter(filter1.and(filter2)).count();
        List result = arrayList.stream().filter(filter1.and(filter2)).collect(Collectors.toList());
        System.out.println("count: " + result.toString());


        List<Integer> collect = Stream.of(20, 18, 79, 99, 66).filter(i -> 30 < i & i < 89).collect(Collectors.toList());
        System.out.println(collect.toString());
    }

    private static void hashMapTest() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put(null, "郭");
        params.put(null, "小军");
        System.out.println(params.get(null));
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap(10);
        map.put("1", "郭小军");
        map.get("1");
        AbstractQueuedSynchronizer abstractQueuedSynchronizer;
        ReentrantLock reentrantLock;
    }

    private static void swapTest() {
        Integer[] intArray = {2, 3, 1, 5};
        String[] strArray = {"赵高", "柳传志", "马云"};

        swap(intArray, 1, 3);
        swap(strArray, 0, 2);

        System.out.println("intArray: " + Arrays.toString(intArray));
        System.out.println("strArray: " + Arrays.toString(strArray));
    }

    public static <T> void swap(T[] t, int i, int j) {
        T temp = t[i];
        t[i] = t[j];
        t[j] = temp;
    }


}
